﻿// -----------------------------------------------------------------------
//  <copyright file="NacosModule.cs" company="LiuliuSoft">
//      Copyright (c) 2025 66SOFT. All rights reserved.
//  </copyright>
//  <site>https://ifs.66soft.net</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2025-10-03 12:10</last-date>
// -----------------------------------------------------------------------

using DaprPlus.AspNetCore;
using DaprPlus.Builder;

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

using Nacos.AspNetCore.V2;
using Nacos.V2.DependencyInjection;


namespace Shares.Infrastructure.Nacos;

public class NacosModule : AspModuleBase
{
    /// <summary>
    /// 获取 模块级别，级别越小越先启动
    /// </summary>
    public override ModuleLevel Level => ModuleLevel.Framework;

    /// <summary>
    /// 将模块服务添加到依赖注入服务容器中
    /// </summary>
    /// <param name="services">依赖注入服务容器</param>
    /// <returns></returns>
    public override IServiceCollection AddServices(IServiceCollection services)
    {
        var builder = services.GetSingletonInstanceOrNull<WebApplicationBuilder>();
        if (builder == null)
        {
            return base.AddServices(services);
        }

        var configuration = builder.Configuration;
        builder.Configuration.AddNacosV2Configuration(configuration.GetSection("Nacos"));
        services.AddNacosAspNet(configuration);
        services.AddNacosV2Config(configuration);
        services.AddNacosV2Naming(configuration);
        return base.AddServices(services);
    }
}

/*
 文档：https://nacos-sdk-csharp.readthedocs.io/en/latest/

 在 appsettings.json 中添加 Nacos 配置：
  "Nacos": {
     "ServerAddresses": [ "http://localhost:8848" ],
     "Namespace": "liuliu-fsb-dev",
     "ServiceName": "SoftwaresApi",
     "Port": 5011,
     "Listeners": [
       {
         "Optional": false,
         "DataId": "appsettings",
         "Group": "SoftwaresApi"
       }
     ]
   }
 */